import 'package:flutter/material.dart';

///todo:[_AppBarCustomState]asdsad
/*
 * @class AppBarCustom
 * @author: ZhongWb
 * @date: 2019/12/17 14:59
 * @description AppBar重写官方  自定义导航
 **/

/*
 * 这是一个可以指定SafeArea区域背景色的AppBar
 * PreferredSizeWidget提供指定高度的方法
 * 如果没有约束其高度，则会使用PreferredSizeWidget指定的高度
 */

class AppBarCustom extends StatefulWidget  implements PreferredSizeWidget {
  final double contentHeight; //从外部指定高度
  final Color statusBarColor; //设置statusbar的颜色
  final List<Widget> right;//右边组件
  final List<Widget> left;//左边组件
  final double leftPadding;//左边距
  final double rightPadding;//右边距
  final String title;//标题
  final Color titleColor;//标题颜色
  final double titleSize;//标题大小
  AppBarCustom({this.title:"标题",this.titleColor:Colors.white,this.titleSize :17,this.contentHeight : 43,this.statusBarColor,this.right,this.left,this.leftPadding:3,this.rightPadding:12});
  @override
  _AppBarCustomState createState() => _AppBarCustomState();

  @override
  Size get preferredSize => new Size.fromHeight(contentHeight);
}

/*
 * 这里没有直接用SafeArea，而是用Container包装了一层
 * 因为直接用SafeArea，会把顶部的statusBar区域留出空白
 * 外层Container会填充SafeArea，指定外层Container背景色也会覆盖原来SafeArea的颜色
 */
class _AppBarCustomState extends State<AppBarCustom> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: widget.statusBarColor ?? Color(0xff3D7ADD),
      child: SafeArea(
        top: true,
        child: Container(
          height: widget.contentHeight,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Container(
                padding: EdgeInsets.only(left: widget.leftPadding),
                height: widget.contentHeight,
                width: 100,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: widget.left ?? [
                    Icon(Icons.chevron_left,color: Colors.white,),
                    Text("返回",style: TextStyle(fontSize: 17,color: Colors.white),),
                  ],
                ),
              ),
              Expanded(child: Center(child: Text(widget.title,style: TextStyle(fontSize: widget.titleSize,color: widget.titleColor),overflow: TextOverflow.ellipsis,),)),
              Container(
                padding: EdgeInsets.only(right: widget.rightPadding),
                height: widget.contentHeight,
                width: 100,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: widget.right ?? [
                    Text("删除",style: TextStyle(fontSize: 17,color: Colors.white),),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
